﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FullThrustLogic.Core;
using FullThrustLogic.ShipComponent;
using FullThrustLogic.ShipComponent.Weapon;

namespace LogicTest.Controls
{
    public partial class PlayerShips : UserControl
    {
        private List<Ship> _ships;

        public Ship SelectedShip
        {
            get
            {
                if(tvPlayerShips.SelectedNode != null)
                {
                    if (tvPlayerShips.SelectedNode.Tag is Ship)
                    {
                        return (Ship) tvPlayerShips.SelectedNode.Tag;
                    }

                    return FindParentShipNode(tvPlayerShips.SelectedNode);

                }

                return null;
            }
        }

        private Ship FindParentShipNode(TreeNode selectedNode)
        {
            object tag = selectedNode.Parent.Tag;
            if(tag is Ship)
            {
                return (Ship)tag;
            }

            return FindParentShipNode(selectedNode.Parent);
        }

        public PlayerShips(List<Ship> ships)
        {
            InitializeComponent();

            _ships = ships;

            LoadShipView();
        }

        private void LoadShipView()
        {
            foreach (Ship ship in _ships)
            {
                TreeNode shipNode = new TreeNode("Ship -> X, " + ship.PositionX.ToString("0") + " Y, " + ship.PositionY.ToString("0")) { Tag = ship };

                if(ship.CurrentMovementOrder != null)
                {
                    shipNode.BackColor = Color.Green;
                }

                if (!ship.AvailableToFire)
                {
                    shipNode.BackColor = Color.Gray;
                }

                tvPlayerShips.Nodes.Add(shipNode);

                AddWeapons(ship, shipNode);

                AddFireControl(ship, shipNode);

                AddThrust(ship, shipNode);

                AddHull(ship, shipNode);
            }
        }

        public void UpdateSelectedShip()
        {


            foreach(TreeNode node in tvPlayerShips.Nodes)
            {
                if (node.Tag == SelectedShip)
                {
                    if (SelectedShip.CurrentMovementOrder != null)
                    {
                        node.BackColor = Color.Green;
                    }

                    if (!SelectedShip.AvailableToFire)
                    {
                        node.BackColor = Color.Gray;
                    }
                }

                foreach (TreeNode fcsNode in node.Nodes[1].Nodes)
                {
                    foreach(FireControlSystem fcs in SelectedShip.FireControlSystems)
                    {
                        if(fcsNode.Tag == fcs)
                        {
                            if(fcs.TargetShip != null)
                            {
                                node.BackColor = Color.Purple;
                                fcsNode.BackColor = Color.Red;

                                foreach(AttackOrder attack in fcs.AttackOrders)
                                {
                                    foreach (TreeNode weaponNode in node.Nodes[0].Nodes)
                                    {
                                        if(attack.TargetingWeapon == weaponNode.Tag)
                                        {
                                            node.BackColor = Color.Purple;
                                            weaponNode.BackColor = Color.Red;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        private void AddHull(Ship ship, TreeNode shipNode)
        {
            TreeNode headerNode = new TreeNode("Hull");
            headerNode.Collapse();


            shipNode.Nodes.Add(headerNode);
            
            foreach (KeyValuePair<int, HullStregth> kvp in ship.Hull.Stregth)
            {
                string nodeLabel = "";

                nodeLabel += "Damage ->" + kvp.Value.CurrentDamage.ToString() + " of " + kvp.Value.MaxDamage.ToString();

                headerNode.Nodes.Add(new TreeNode(nodeLabel) { Tag = kvp.Value });

            }
        }

        private void AddThrust(Ship ship, TreeNode shipNode)
        {
            TreeNode headerNode = new TreeNode("Thrusters");
            headerNode.Collapse();


            shipNode.Nodes.Add(headerNode);


            foreach (Thrust item in ship.Thrusts)
            {
                string nodeLabel = "";

                nodeLabel += item.AvailableThrust.ToString() + "Available Thrust, ";

                if (item.Enabled)
                {
                    nodeLabel += "Enabled,";
                }
                else
                {
                    nodeLabel += "Disabled,";
                }

                headerNode.Nodes.Add(new TreeNode(nodeLabel) { Tag = item });

            }
        }


        private void AddFireControl(Ship ship, TreeNode shipNode)
        {
            TreeNode fcsHeaderNode = new TreeNode("Fire Control Systems");
            fcsHeaderNode.Collapse();


            shipNode.Nodes.Add(fcsHeaderNode);


            foreach(FireControlSystem fcs in ship.FireControlSystems)
            {
                string fscString = "";

                if (fcs.Enabled)
                {
                    fscString = "Enabled,";

                    if (fcs.TargetShip != null)
                    {
                        fscString += "Target Engaged";
                    }
                    else
                    {
                        fscString += "Free To Fire";
                    }

                    TreeNode fscNode = new TreeNode(fscString){Tag = fcs};

                    if (fcs.TargetShip != null)
                    {
                        fscNode.BackColor = Color.Red;
                    }

                    fcsHeaderNode.Nodes.Add(fscNode);
                }
                else
                {
                    fscString = "Disabled,";
                    fcsHeaderNode.Nodes.Add(new TreeNode(fscString){Tag = fcs});
                }


            }
        }

        private void AddWeapons(Ship ship, TreeNode shipNode)
        {
            TreeNode weaponHeaderNode = new TreeNode("Weapons");

            weaponHeaderNode.Collapse();

            shipNode.Nodes.Add(weaponHeaderNode);

            foreach(iWeapon weapon in ship.Weapons)
            {
                string weaponString = "Beam,";
                     
                if(weapon is Torpedo)
                {
                    weaponString = "Torpedo,";
                }

                if(weapon.Enabled)
                {
                    weaponString += "Enabled,";
                }
                else
                {
                    weaponString += "Disabled,";
                }

                TreeNode weaponNode = new TreeNode(weaponString){Tag=weapon};

                weaponHeaderNode.Nodes.Add(weaponNode);
            }
        }
    }
}
